import 'dart:convert';

import 'package:flutterclue/utils/SharePreferensUtil.dart';

class UtilsString {
  /// tag  year:精确到年
  ///    month:精确到月
  ///    day:精确到日
  ///    hour:精确到时
  ///    minute:精确到分
  ///    sec:精确到秒
  String timeFormat(DateTime val, String tag) {
    String timestamp = "";
    if (tag == "year") {
      timestamp = "${val.year.toString()}";
    } else if (tag == "month") {
      timestamp =
          "${val.year.toString()}-${val.month.toString().padLeft(2, '0')}";
    } else if (tag == "day") {
      timestamp =
          "${val.year.toString()}-${val.month.toString().padLeft(2, '0')}-${val.day.toString().padLeft(2, '0')} ";
    } else if (tag == "hour") {
      timestamp =
          "${val.year.toString()}-${val.month.toString().padLeft(2, '0')}-${val.day.toString().padLeft(2, '0')} ${val.hour.toString().padLeft(2, '0')}";
    } else if (tag == "minute") {
      timestamp =
          "${val.year.toString()}-${val.month.toString().padLeft(2, '0')}-${val.day.toString().padLeft(2, '0')} ${val.hour.toString().padLeft(2, '0')}:${val.minute.toString().padLeft(2, '0')}";
    } else if (tag == "sec") {
      timestamp =
          "${val.year.toString()}-${val.month.toString().padLeft(2, '0')}-${val.day.toString().padLeft(2, '0')} ${val.hour.toString().padLeft(2, '0')}:${val.minute.toString().padLeft(2, '0')}:${val.second.toString().padLeft(2, '0')}";
    }
    return timestamp;
  }

  ///根据权限判断角色
  static Future<bool> getAuthority(String role) async {
    String tolelist =
        await SharePreferencesUtil.getString(SharePreferencesUtil.FunctionCode);
    List list = json.decode(tolelist);
    for (var i = 0; i < list.length; i++) {
      String Authority = list[i]["FunctionCode"].toString();
      if (Authority == role) {
        return true;
      }
    }
    return false;
  }
}
